<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Order\Services\OrderService;
use App\Modules\Shop\Models\GoodsCategories;
use App\Modules\Shop\Models\OrderProducts;
use App\Modules\Shop\Models\Products;
use App\Modules\Shop\Models\ProductSpecifiction;
use App\Modules\Shop\Models\Ships;
use App\Modules\Shop\Services\GoodsCategoriesService;
use App\Modules\Shop\Services\OrderProductsService;
use App\Modules\Shop\Services\ProductsService;
use App\Modules\Shop\Services\ProductsSpecifictionService;
use App\Modules\Shop\Services\ShipFeeTemplateService;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Image;
use DB;

class OrdersController extends Controller
{
    public function ordersList(Request $request) {

        #return view('orders_list',compact('orders'));
        return Admin::content(function (Content $content) use($request){
            $status = $request->input('status','all');
            $trade_no = $request->input('trade_no','');
            $transaction_id = $request->input('transaction_id','');
            $phone = $request->input('phone','');
            $start_time = $request->input('start_time','');
            $end_time = $request->input('end_time','');
            $orders = OrderService::getOrderAndSubOrderLists(compact('status','trade_no','transaction_id','phone','start_time','end_time'),false,5);
            $ship_companys = \DB::table('ship_companies')->where('status',1)->get();
            $provinces = \DB::table('area')->where('level',1)->get();
            $content->body(view('shop.orders_list',compact('orders','ship_companys','provinces')));
        });
    }

    public function orderDetail(Request $request) {
        return Admin::content(function (Content $content) use($request){
            $order_product_id = $request->input('order_product_id');
            $order_product = OrderProductsService::getById($order_product_id);
            switch ($order_product->status){
                case 0:$order_product->status='待支付';break;
                case 1:$order_product->status='待备货';break;
                case 2:$order_product->status='支付失败';break;
                case 3:$order_product->status='退款';break;
                case 4:$order_product->status='待发货';break;
                case 5:$order_product->status='已发货';break;
                case 6:$order_product->status='已收货';break;

            }
            $content->body(view('shop.order_detail',compact('order_product')));
        });
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function prepare_goods(Request $request) {
        $order_product_id = $request->input('order_product_id','');
        $order_id = $request->input('order_id','');

        if(empty($order_product_id)&&empty($order_id)) {
            return response()->error('PARAM_EMPTY');
        }
        \DB::beginTransaction();
        try {
            if($order_id){
                $order_products = OrderProducts::where('order_id',$order_id)->get();
                foreach ($order_products as $order_product) {
                    if($order_product->status !=1) {
                        \DB::rollBack();
                        return ['code'=>30002,'msg'=>'订单状态不是已支付状态'];
                    }
                    $order_product->status = 4;
                    $order_product->save();
                }
            }
            if($order_product_id) {
                $order_product = OrderProductsService::getById($order_product_id);
                if($order_product->status !=1) {
                    \DB::rollBack();
                    return ['code'=>30002,'msg'=>'订单状态不是已支付状态'];
                }
                $order_product->status = 4;
                $order_product->save();
            }
            \DB::commit();
        }catch (\Exception $exception) {
            \DB::rollBack();
            \Log::error(__FUNCTION__.$exception->getMessage());
            return response()->error('PREPARE_FAILED');
        }

        return response()->success();
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function deliver_goods(Request $request) {
        $order_product_id = $request->input('order_product_id','');
        $order_id = $request->input('order_id','');
        $ship_company_id = $request->input('ship_company_id','');
        $ship_order_sn = $request->input('ship_order_sn','');
        if((empty($order_product_id) && empty($order_id))||empty($ship_company_id)||empty($ship_order_sn)) {
            return response()->error('PARAM_EMPTY');
        }
        \DB::beginTransaction();
        try{
            if($order_product_id) {
                $order_product = OrderProductsService::getById($order_product_id);
                $orders = OrderService::findById($order_product->order_id);
                $ship_company = \DB::table('ship_companies')->where('id',$ship_company_id)->first();
                $data = array(
                    //'order_product_id'=>$order_product_id,
                    'ship_company_id'=>$ship_company_id,
                    'ship_company_name'=>$ship_company->name,
                    'ship_order_sn'=>$ship_order_sn,
                    'uid'=>$order_product->uid,
                    'address' => $orders->address->address,
                    'region_id' => $orders->address->region_id,
                    'city_id' => $orders->address->city_id,
                    'province_id' => $orders->address->province_id,
                    'area_name' => $orders->address->area_name,
                    'target'=>$orders->address->consignee,
                    'phone'=>$orders->address->phone
                );
                $ships = Ships::create($data);
                if($order_product->status !=4) {
                    \DB::rollBack();
                    return ['code'=>3001,'msg'=>''];
                }
                $order_product->ship_id = $ships->id;
                $order_product->deliver_time = date('Y-m-d H:i:s');
                $order_product->status = 5;
                $order_product->save();
            }elseif ($order_id) {
                $orders = OrderService::findById($order_id);
                $orders->is_batch_ship = 1;
                $order_products = OrderProducts::where('order_id',$order_id)->get();
                $ship_company = \DB::table('ship_companies')->where('id',$ship_company_id)->first();
                $data = array(
                    //'order_product_id'=>$order_product_id,
                    'ship_company_id'=>$ship_company_id,
                    'ship_company_name'=>$ship_company->name,
                    'ship_order_sn'=>$ship_order_sn,
                    'uid'=>$orders->uid,
                    'address' => $orders->address->address,
                    'region_id' => $orders->address->region_id,
                    'city_id' => $orders->address->city_id,
                    'province_id' => $orders->address->province_id,
                    'area_name' => $orders->address->area_name,
                    'target'=>$orders->address->consignee,
                    'phone'=>$orders->address->phone
                );
                $orders->save();
                $ships = Ships::create($data);
                foreach ($order_products as $order_product) {
                    if($order_product->status !=4) {
                        \DB::rollBack();
                        return ['code'=>3001,'msg'=>'该订单不是已备货状态'];
                    }
                    $order_product->ship_id = $ships->id;
                    $order_product->status = 5;
                    $order_product->save();
                }
            }
            \DB::commit();
        }catch (\Exception $e) {
            \DB::rollBack();
            \Log::error(__FUNCTION__.':'.$e->getMessage());
            return response()->error('DELIVER_FAILED');
        }
        return response()->success();
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function getGeneralAddress(Request $request) {
        $level = $request->input('level','');
        $parent_id = $request->input('parent_id','');
        $res = \DB::table('area')->where('level',$level)->where('parent_id',$parent_id)->get();
        return response()->success($res);
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function modifyAddress(Request $request) {
        $order_product_id = $request->input('order_product_id','');
        $order_id = $request->input('order_id','');
        $province_id = $request->input('province_id','');
        $city_id = $request->input('city_id','');
        $region_id = $request->input('region_id','');
        $address = $request->input('address','');
        $contact = $request->input('contact','');
        $phone = $request->input('phone','');
        if((empty($order_product_id)&&empty($order_id))||empty($province_id)||empty($city_id)||empty($region_id)||empty($address)||empty($contact)||empty($phone)) {
            return response()->error('PARAM_EMPTY');
        }

        $areas = \DB::table('area')->whereIn('id',[$province_id,$city_id,$region_id])->get();
        $province_name = $city_name = $region_name = '';
        foreach ($areas as $area) {
            if($area->id == $province_id) {
                $province_name = $area->name;
            }
            if($area->id == $city_id) {
                $city_name = $area->name;
            }
            if($area->id == $region_id) {
                $region_name = $area->name;
            }

        }
        $area_name = $province_name.$city_name.$region_name.$address;

        if($order_id) {
            $order_info = OrderService::findById($order_id);
            if(!$order_info->is_batch_ship) {
                return ['code'=>304,'msg'=>'不是批量发货的订单'];
            }
            $order_product_info = OrderProducts::where('order_id',$order_id)->first();
        }

        if($order_product_id) {
            $order_product_info = OrderProductsService::getById($order_product_id);
        }

        $ship_info = Ships::where('id',$order_product_info->ship_id)->first();
        $ship_info->province_id = $province_id;
        $ship_info->city_id = $city_id;
        $ship_info->region_id = $region_id;
        $ship_info->address = $address;
        $ship_info->area_name = $area_name;
        $ship_info->target = $contact;
        $ship_info->phone = $phone;
        $ship_info->save();
        return response()->success();
    }


}
